package org.hsqldb.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public final class HSQLDB_v2_0_0_rc8_Demo
{
	private static void __select_version1(
			final Connection pConn )
			throws SQLException
	{
		try {
			System .out .println( "### VERSION 1 (prepared statement) ###" ); System .out .flush();

			// retrieve data using a prepared statement and a dynamic argument
			PreparedStatement pstmt = pConn.prepareStatement(
					"SELECT COUNT( * ) FROM T1 WHERE VALUE1 = ? " );
			pstmt.setString( 1, "hy, hsqldb!" );
			ResultSet rset = pstmt.executeQuery();
			if ( rset.next() ) {
				;
			}
			pstmt.close();

			pConn.commit();
		}
		catch ( SQLException ex ) {
			pConn.rollback();
			ex.printStackTrace( System.err );
		}
	}

	private static void __select_version2(
			final Connection pConn )
			throws SQLException
	{
		try {
			System .out .println( "### VERSION 2 (statement) ###" ); System .out .flush();

			// retrieve data using a statement and a constant argument
			Statement pstmt = pConn.createStatement();
			ResultSet rset = pstmt.executeQuery( "SELECT COUNT( * ) FROM T1 WHERE VALUE1 = 'hy, hsqldb!' " );
			if ( rset.next() ) {
				;
			}
			pstmt.close();
			pConn.commit();
		}
		catch ( SQLException ex ) {
			pConn.rollback();
			ex.printStackTrace( System.err );
		}
	}

	public static void main(
			final String[] pArgs )
			throws Throwable
	{
		// Preparation: connect to server, create 1 table
		Class.forName( "org.hsqldb.jdbcDriver" );
		Connection conn = DriverManager.getConnection( "jdbc:hsqldb:mem:aname", "sa", "" );
		Statement stmt = conn.createStatement();
		stmt.executeUpdate( "CREATE TABLE T1 ( VALUE1 VARCHAR( 4 ) )" );
		System .out .println( "table created" );
		stmt.close();
		conn.commit();

		// select something from table
		__select_version1( conn );
		__select_version2( conn );

		// finished
		conn.close();
		System .out .println( "ready" );
	}

	// just for making checkstyle silent.
	private HSQLDB_v2_0_0_rc8_Demo() {}
}
